import $request from './request'
import $env from '../model/env'
import $api from '../model/api'

const apiMap = { ...$api }
const routeUpload = '//upload.yangshipin.cn'

const routeHaccess = $env.enableTest ? '//h5api.yangshipin.cn/web' : '//h5access.yangshipin.cn/web'

// 活动
Object.assign(apiMap, {
	'modify_user': routeHaccess + '/modify_user?vappid=67988023&vsecret=881bb45da6c2827a186f5a6983a2cd2a3b256e370a205acf&raw=1',
	'modify_uinfo': routeHaccess + '/h_modify_user?vappid=59306155&vsecret=b42702bf7309a179d102f3d51b1add2fda0bc7ada64cb801&raw=1',
	'credentials': routeHaccess + '/face_fusion_credentials',
	'place': '//apis.map.qq.com/ws/geocoder/v1/',
	'image_share_upload': routeUpload + `/image_upload`
})

const ActiveApi = {}

// 修改用户信息(免审)
// @param nick
// @param imgPath 相对路径
// @param para.modifyFlag 1只修改了昵称，昵称必填，头像可为""  2只修改了头像，昵称可为""，头像必填，上传的path 3 两个都修改了，两个必填
// @param code jsbirdge.getCode 获取的的 code，确保请求来自客户端内
ActiveApi.modifyUinfo = para => {
	let json = {
		...para
	}
	json = JSON.stringify(json)
	return $request({
		url: apiMap['modify_uinfo'],
		type: 'post',
		processData: false,
		contentType: 'application/json',
		data: json
	})
}

// 修改用户信息
// @param nick
// @param imgPath 相对路径
// @param para.modifyFlag 1只修改了昵称，昵称必填，头像可为""  2只修改了头像，昵称可为""，头像必填，上传的path 3 两个都修改了，两个必填
ActiveApi.modifyUser = para => {
	let json = {
		...para
	}
	json = JSON.stringify(json)
	return $request({
		url: apiMap['modify_user'],
		type: 'post',
		processData: false,
		contentType: 'application/json',
		data: json
	})
}

// 获取上传凭据
ActiveApi.credentials = para => $request({
	url: apiMap['credentials'],
	type: 'get',
	data: {
		vappid: '59306155',
		vsecret: 'b42702bf7309a179d102f3d51b1add2fda0bc7ada64cb801',
		raw: 1,
		...para
	}
})

// 上传base64图片返回普通地址接口
ActiveApi.baseImageUpload = (data) => $request({
	url: apiMap['image_share_upload'],
	type: 'post',
	contentType: 'application/json',
	data
})

// 获取地理位置
ActiveApi.location = coords => {
	coords = coords || {}
	let para = {
		key: '2KDBZ-66C3F-B3CJP-NTMBU-UIGKO-OFB7S',
		output: 'jsonp',
		location: `${coords.latitude},${coords.longitude}`
	}
	return $request({
		url: apiMap.place,
		dataType: 'jsonp',
		data: para,
		formatResult (res) {
			let rs = {}
			rs.ret = res.status
			rs.msg = res.message
			rs.data = res.result
			return rs
		}
	})
}

export default ActiveApi
